package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.WxuserInfoAddNewDTO;
import cn.tedu.truetaste.pojo.dto.WxuserInfoUpdateDTO;
import cn.tedu.truetaste.pojo.vo.WxuserInfoListItemVO;
import cn.tedu.truetaste.pojo.vo.WxuserInfoStandardVO;
import cn.tedu.truetaste.service.IWxuserInfoService;
import cn.tedu.truetaste.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理用户数据相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "用户详情管理模块")
@Slf4j
@RestController
@RequestMapping("/wx/user")
public class WxuserInfoController {

    @Autowired
    private IWxuserInfoService wxuserInfoService;

    public WxuserInfoController() {
        log.info("创建控制器对象：WxuserInfoController");
    }

    @ApiOperation("添加用户详情")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(WxuserInfoAddNewDTO wxuserInfoAddNewDTO) {
        log.debug("开始处理【添加用户详情】的请求，参数：{}", wxuserInfoAddNewDTO);
        wxuserInfoService.addNew(wxuserInfoAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/user/9527/update
    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "openId", value = "用户详情openId", required = true, dataType = "long")
    @PostMapping("/{openId:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable String openId, WxuserInfoUpdateDTO wxuserInfoUpdateDTO) {
        log.debug("开始处理【修改用户详情】的请求，参数openId：{}, 新数据：{}", openId, wxuserInfoUpdateDTO);
        wxuserInfoService.updateInfoById(openId, wxuserInfoUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/user/9527
    @ApiOperation("根据openId查询用户详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{openId:[0-9]+}")
    public JsonResult<WxuserInfoStandardVO> getStandardByOpenId(@PathVariable String openId) {
        log.debug("开始处理【根据openId查询用户详情】的请求，参数：{}", openId);
        WxuserInfoStandardVO wxuserInfoStandardVO = wxuserInfoService.getStandardByOpenId(openId);
        return JsonResult.ok(wxuserInfoStandardVO);
    }

    // http://localhost:9989/user
    @ApiOperation("查询用户详情列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<WxuserInfoListItemVO>> list() {
        log.debug("开始处理【查询用户详情列表】的请求，无参数");
        List<WxuserInfoListItemVO> list = wxuserInfoService.list();
        return JsonResult.ok(list);
    }

}
